<!doctype html>
<html lang="en">
<head>
	<title>Three.js -- Template</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
	<style>
		body 
		{
			font-family: Monospace;
			font-weight: bold;
			background-color: #000000;
			margin: 0px;
			overflow: hidden;
		}
	</style>
</head>
<body>

<div id="message"></div>

<script src="js/Three62.js"></script>
<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>
<script src="js/TrackballControls.js"></script>
<script src="js/KeyboardState.js"></script>
<script src="js/THREEx.FullScreen.js"></script>
<script src="js/THREEx.WindowResize.js"></script>


<!-- ----------------------------------------------------------- -->

<script>
/*
	Three.js "tutorials by example"
	Author: Lee Stemkoski
 */

// MAIN

// standard global variables
var container, scene, camera, renderer, controls, stats;

init();
animate();

// FUNCTIONS 		
function init() 
{
	// SCENE
	scene = new THREE.Scene();
	// CAMERA
	var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
	var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
	camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
	scene.add(camera);
	camera.position.set(0,150,400);
	camera.lookAt(scene.position);	
	// RENDERER
	if ( Detector.webgl )
		renderer = new THREE.WebGLRenderer( {antialias:true} );
	else
		renderer = new THREE.CanvasRenderer(); 
	renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
	container = document.createElement( 'div' );
	document.body.appendChild( container );
	container.appendChild( renderer.domElement );
	// EVENTS
	THREEx.WindowResize(renderer, camera);
	THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
	// CONTROLS
	controls = new THREE.TrackballControls( camera, renderer.domElement );
	// STATS
	stats = new Stats();
	stats.domElement.style.position = 'absolute';
	stats.domElement.style.bottom = '0px';
	stats.domElement.style.zIndex = 100;
	container.appendChild( stats.domElement );
	// LIGHT
	var light = new THREE.PointLight(0xffffff);
	light.position.set(0,250,0);
	scene.add(light);
	
	 
	////////////
	// CUSTOM //
	////////////

    var sphereGeo = new THREE.SphereGeometry(100, 64, 32);
	
    // Create the Earth with nice texture

	var colors = THREE.ImageUtils.loadTexture( "images/earth-day.jpg" );

	var earthMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff, map: colors } );

    this.earthSphere = new THREE.Mesh( sphereGeo, earthMaterial ); 
    scene.add(earthSphere);	
	
    renderer.autoClear = false;
    renderer.setClearColor(0x000000, 0.0);
	
	addMarker(20, -160, 0xFF0000); // Near Hawaii

	addMarker(40.7, -73.6, 0x0000FF); // Garden City, NY
	var GCNY = convertLatLonToVec3(40.7, -73.6).multiplyScalar(100.5);
	addMarker(30,-90, 0x00FF00); // New Orleans, LA
	var NOLA = convertLatLonToVec3(30,-90).multiplyScalar(100.5);
	
	drawCurve( createSphereArc(GCNY,NOLA), 0x00FFFF );
}

function addMarker(lat, lon, colory)
{
	var radius = 100;
	var marker = new THREE.Mesh( new THREE.SphereGeometry(1, 8, 4), new THREE.MeshBasicMaterial({color:colory}) );
	marker.position = convertLatLonToVec3(lat,lon).multiplyScalar(radius);
	scene.add(marker)
}


function convertLatLonToVec3(lat,lon)
{
	lat =  lat * Math.PI / 180.0;
	lon = -lon * Math.PI / 180.0;
	return new THREE.Vector3( 
		Math.cos(lat) * Math.cos(lon), 
		Math.sin(lat), 
		Math.cos(lat) * Math.sin(lon) );
}

function greatCircleFunction(P, Q)
{
	var angle = P.angleTo(Q);
	return function(t)
	{
	    var X = new THREE.Vector3().addVectors(
			P.clone().multiplyScalar(Math.sin( (1 - t) * angle )), 
			Q.clone().multiplyScalar(Math.sin(      t  * angle )))
			.divideScalar( Math.sin(angle) );
	    return X;
	};
}

function createSphereArc(P,Q)
{
	var sphereArc = new THREE.Curve();
	sphereArc.getPoint = greatCircleFunction(P,Q);
	return sphereArc;
}

function drawCurve(curve, color)
{
	var lineGeometry = new THREE.Geometry();
	lineGeometry.vertices = curve.getPoints(100);
	lineGeometry.computeLineDistances();
	var lineMaterial = new THREE.LineBasicMaterial();
	lineMaterial.color = (typeof(color) === "undefined") ? new THREE.Color(0xFF0000) : new THREE.Color(color);
	var line = new THREE.Line( lineGeometry, lineMaterial );
	scene.add(line);
}

function animate() 
{
    requestAnimationFrame( animate );
	render();		
	update();
}

function update()
{
	controls.update();
	stats.update();
}

function render() 
{
	renderer.clear();
    renderer.render(scene, camera);
}

</script>

</body>
</html>
